<div class="container-xl">
  <!-- Page title -->
  <div class="page-header d-print-none">
    <div class="row align-items-center">
      <div class="col">
        <h2 class="page-title">
          历史记录
        </h2>
      </div>
      <div class="col-auto ms-auto d-print-none">
        <div class="btn-list">
          <a href="javascript:batch_check_action('reidentification')"
             class="btn btn-primary ms-auto d-none d-sm-inline-block">
            <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-refresh" width="24" height="24"
                 viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
                 stroke-linejoin="round">
              <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
              <path d="M20 11a8.1 8.1 0 0 0 -15.5 -2m-.5 -4v4h4"></path>
              <path d="M4 13a8.1 8.1 0 0 0 15.5 2m.5 4v-4h-4"></path>
            </svg>
            重新识别
          </a>
          <a href="javascript:batch_check_action('reidentification')" class="btn btn-primary d-sm-none btn-icon">
            <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-refresh" width="24" height="24"
                 viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
                 stroke-linejoin="round">
              <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
              <path d="M20 11a8.1 8.1 0 0 0 -15.5 -2m-.5 -4v4h4"></path>
              <path d="M4 13a8.1 8.1 0 0 0 15.5 2m.5 4v-4h-4"></path>
            </svg>
          </a>
          <a href="javascript:batch_check_action('delete')" class="btn btn-danger ms-auto d-none d-sm-inline-block">
            <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-trash" width="24" height="24"
                 viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
                 stroke-linejoin="round">
              <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
              <line x1="4" y1="7" x2="20" y2="7"></line>
              <line x1="10" y1="11" x2="10" y2="17"></line>
              <line x1="14" y1="11" x2="14" y2="17"></line>
              <path d="M5 7l1 12a2 2 0 0 0 2 2h8a2 2 0 0 0 2 -2l1 -12"></path>
              <path d="M9 7v-3a1 1 0 0 1 1 -1h4a1 1 0 0 1 1 1v3"></path>
            </svg>
            批量删除
          </a>
          <a href="javascript:batch_check_action('delete')" class="btn btn-danger d-sm-none btn-icon">
            <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-trash" width="24" height="24"
                 viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
                 stroke-linejoin="round">
              <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
              <line x1="4" y1="7" x2="20" y2="7"></line>
              <line x1="10" y1="11" x2="10" y2="17"></line>
              <line x1="14" y1="11" x2="14" y2="17"></line>
              <path d="M5 7l1 12a2 2 0 0 0 2 2h8a2 2 0 0 0 2 -2l1 -12"></path>
              <path d="M9 7v-3a1 1 0 0 1 1 -1h4a1 1 0 0 1 1 1v3"></path>
            </svg>
          </a>
        </div>
      </div>
      <!-- Page title actions -->
    </div>
  </div>
</div>
<!-- 业务页面代码 -->
<div class="page-body">
  <div class="container-xl">
    <div class="row row-cards">
      <div class="col-12">
        <div class="card">
          <div class="card-body border-bottom py-3">
            <div class="d-flex">
              <div class="text-muted">
                共 {{ TotalCount }} 条记录
              </div>
              <div class="ms-auto text-muted">
                搜索:
                <div class="ms-2 d-inline-block">
                  <input id="search_word" value="{{ Search }}" type="text" class="form-control form-control-sm"
                         aria-label="搜索">
                </div>
              </div>
            </div>
          </div>
          <div class="table-responsive" style="min-height: 300px">
            <table class="table table-vcenter card-table">
              <thead>
              <tr>
                {% if TotalCount > 0 %}
                  <th class="w-1"><input class="form-check-input m-0 align-middle" id="history_check_all_btn"
                                         type="checkbox" aria-label="全选"></th>
                {% endif %}
                <th>媒体信息</th>
                <th>文件信息</th>
                <th>时间</th>
                <th></th>
              </tr>
              </thead>
              <tbody>
              {% if TotalCount > 0 %}
                {% for History in Historys %}
                  <tr>
                    <td class="w-1"><input class="form-check-input m-0 align-middle" id="check_{{ History.ID }}"
                                           type="checkbox"></td>
                    <td>
                      <div class="d-flex py-1 align-items-center">
                      <span class="avatar me-2 text-nowrap">
                        {% if History.TYPE == "电影" %}
                          <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-movie" width="24"
                               height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none"
                               stroke-linecap="round" stroke-linejoin="round">
                          <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                          <rect x="4" y="4" width="16" height="16" rx="2"></rect>
                          <line x1="8" y1="4" x2="8" y2="20"></line>
                          <line x1="16" y1="4" x2="16" y2="20"></line>
                          <line x1="4" y1="8" x2="8" y2="8"></line>
                          <line x1="4" y1="16" x2="8" y2="16"></line>
                          <line x1="4" y1="12" x2="20" y2="12"></line>
                          <line x1="16" y1="8" x2="20" y2="8"></line>
                          <line x1="16" y1="16" x2="20" y2="16"></line>
                        </svg>
                        {% else %}
                          <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-device-tv"
                               width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"
                               fill="none"
                               stroke-linecap="round" stroke-linejoin="round">
                          <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                          <rect x="3" y="7" width="18" height="13" rx="2"></rect>
                          <polyline points="16 3 12 7 8 3"></polyline>
                        </svg>
                        {% endif %}
                      </span>
                        <div class="flex-fill">
                          <div class="font-weight-medium">
                            {% if History.TMDBID %}
                              <a href="https://www.themoviedb.org/{% if History.TYPE == "电影" %}movie{% else %}tv{% endif %}/{{ History.TMDBID }}"
                                 target="_blank">
                              {{ History.TITLE }} ({{ History.YEAR }})
                              </a>
                              {% if History.SEASON_EPISODE %}
                              <br><a href="https://www.themoviedb.org/tv/{{ History.TMDBID }}{{ History.SEASON_EPISODE.replace('S', '/season/').replace('E', '/episode/')  }}"
                               target="_blank">
                              <span class="text-orange">{{ History.SEASON_EPISODE }}</span></a>
                              {% endif %}
                            {% else %}
                              {{ History.TITLE }} ({{ History.YEAR }})
                              {% if History.SEASON_EPISODE %}
                              <span class="text-orange">{{ History.SEASON_EPISODE }}</span>
                              {% endif %}
                            {% endif %}
                          </div>
                          {% if History.CATEGORY %}
                            <div class="text-muted">类别：{{ History.CATEGORY }}</div>
                          {% endif %}
                        </div>
                      </div>
                    </td>
                    <td>
                      <div>
                        <a href='javascript:navmenu("mediafile?dir={{ History.SOURCE_PATH.replace("\\", "/") }}")'>
                          {{ History.SOURCE_FILENAME or '' }}</a>
                      </div>
                      <div>
                        {% if History.DEST_PATH or History.DEST_FILENAME %}
                        <span class="text-muted">=> </span>
                        {% endif %}
                        {% if History.DEST_PATH %}
                          <a class="text-green"
                             href='javascript:navmenu("mediafile?dir={{ History.DEST_PATH.replace("\\", "/") }}")'>
                            {{ History.DEST_FILENAME or '' }}
                          </a>
                        {% else %}
                          {{ History.DEST_FILENAME or '' }}
                        {% endif %}
                      </div>
                    </td>
                    <td>{{ History.DATE }}
                      <div class="text-muted">来自：{{ History.SOURCE or '' }}</div>
                    </td>
                    <td>
                      <div class="dropdown">
                        <a href="#" class="btn-action" data-bs-toggle="dropdown" aria-expanded="false">
                          <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24"
                               viewBox="0 0 24 24"
                               stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
                               stroke-linejoin="round">
                            <path stroke="none" d="M0 0h24v24H0z" fill="none"/>
                            <circle cx="12" cy="12" r="1"/>
                            <circle cx="12" cy="19" r="1"/>
                            <circle cx="12" cy="5" r="1"/>
                          </svg>
                        </a>
                        <div class="dropdown-menu dropdown-menu-end">
                          <a class="dropdown-item"
                             href='javascript:single_re_identification_history("{{ History.ID }}",)'>
                            重新识别
                          </a>
                          <a class="dropdown-item"
                             href='javascript:rename_history("{{ History.TYPE }}", "{{ History.SOURCE_FILENAME }}", "{{ History.ID }}")'>
                            手动识别
                          </a>
                          <a class="dropdown-item text-danger"
                             href='javascript:single_delete_history("{{ History.ID }}", "{{ History.TITLE }} ({{ History.YEAR }}) {{ History.SEASON_EPISODE }}")'>
                            删除
                          </a>
                        </div>
                      </div>
                    </td>
                  </tr>
                {% endfor %}
              {% else %}
                <tr>
                  <td colspan="4" align="center">没有数据</td>
                </tr>
              {% endif %}
              </tbody>
            </table>
          </div>
          {% if TotalCount > 0 %}
            <div class="card-footer d-flex align-items-center">
              <p class="m-0 text-muted">当前页 <span>{{ Count }}</span> 条</p>
              <ul class="pagination m-0 ms-auto">
                <li class="page-item {% if CurrentPage==1 %} disabled {% endif %}">
                  <a class="page-link" href="javascript:go_pre_page('{{ Search }}', {{ CurrentPage }})" tabindex="-1"
                     aria-disabled="true">
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24"
                         stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
                         stroke-linejoin="round">
                      <path stroke="none" d="M0 0h24v24H0z" fill="none"/>
                      <polyline points="15 6 9 12 15 18"/>
                    </svg>
                  </a>
                </li>
                {% for page in PageRange %}
                  <li class="page-item {% if page==CurrentPage %} active {% endif %}">
                    <a class="page-link"
                       href="javascript:navmenu('history?s={{ Search }}&page={{ page }}')">{{ page }}</a>
                  </li>
                {% endfor %}
                <li class="page-item {% if CurrentPage >= TotalPage %} disabled {% endif %}">
                  <a class="page-link"
                     href="{% if CurrentPage < TotalPage %}javascript:go_next_page('{{ Search }}', {{ CurrentPage }}){% else %}javascript:void(0){% endif %}">
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24"
                         stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
                         stroke-linejoin="round">
                      <path stroke="none" d="M0 0h24v24H0z" fill="none"/>
                      <polyline points="9 6 15 12 9 18"/>
                    </svg>
                  </a>
                </li>
              </ul>
            </div>
          {% endif %}
        </div>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript">
  // 上一页
  function go_pre_page(search, page) {
    navmenu("history?s=" + search + "&page=" + (page - 1))
  }

  // 下一页
  function go_next_page(search, page) {
    navmenu("history?s=" + search + "&page=" + (page + 1))
  }

  //手动重新识别
  function rename_history(type, path, id) {
    show_manual_transfer_modal(2, path, '', type, '', id);
  }

  //删除
  function delete_history(logids, name) {
    show_confirm_modal(name + " 对应已重命名的文件将被同步删除，是否确认？", function () {
      hide_confirm_modal();
      show_wait_process();
      ajax_post("delete_history", {"logids": logids}, function (ret) {
        hide_wait_process();
        navmenu('history');
      });
    });
  }

  //重新识别
  function re_identification(logids) {
    show_wait_process();
    ajax_post("re_identification", {flag: "history", ids: logids}, function (ret) {
      hide_wait_process();
      if (ret.retcode == 0) {
        navmenu('history');
      } else {
        show_fail_modal(`重新识别失败：${ret.retmsg}！`);
      }
    });
  }

  // 搜索按钮
  $('#search_word').bind('keypress', function (event) {
    if (event.keyCode == "13") {
      var keyword = $("#search_word").val();
      navmenu("history?s=" + keyword);
    }
  });

  //单选框事件
  $('input[type=radio][name=rename_type]').change(function () {
    if (this.value == 'MOV') {
      $("#rename_season_div").hide();
      $("#rename_episode_div").hide();
      $("#rename_season").val("");
    } else {
      $("#rename_season_div").show();
      $("#rename_episode_div").show();
    }
  });

  //全选事件
  $("#history_check_all_btn").change(function () {
    $(".table-responsive input[type=checkbox]").prop("checked", $(this).prop("checked"));
  });

  // 批量删除/重新识别
  function batch_check_action(flag) {
    var logids = [];
    $("[id^='check_']").each(function () {
      if ($(this).prop("checked")) {
        logids.push($(this).attr("id").replace("check_", ""));
      }
    });
    if (logids.length == 0) {
      return;
    }
    if (flag == "delete") {
      delete_history(logids, "");
    } else if (flag == "reidentification") {
      re_identification(logids);
    } else {
      return;
    }
  }

  //单条记录删除
  function single_delete_history(logid, name) {
    var logids = [logid];
    delete_history(logids, name);
  }

  //单条记录重新识别
  function single_re_identification_history(logid) {
    var logids = [logid];
    re_identification(logids);
  }

</script>